import 'package:flutter/material.dart';
import 'package:library_base/constant/constant.dart';
import 'package:library_base/entities/enum_entity.dart';
import 'package:library_base/entities/stock_out_child_entity.dart';
import 'package:library_base/global/multi_language.dart';
import 'package:library_base/res/colors.dart';
import 'package:library_base/res/decoration.dart';
import 'package:library_base/res/dividing_line.dart';
import 'package:library_base/res/gaps.dart';
import 'package:library_base/res/styles.dart';
import 'package:library_base/res/widget_generator.dart';
import 'package:library_base/utils/theme_util.dart';
import 'package:library_base/ext/theme_data_ext.dart';
import 'package:library_base/res/edge.dart';
import 'package:get/get.dart';
import 'package:library_base/widget/button/gradient_button.dart';
import 'package:library_base/widget/button/ink_bg_well.dart';
import 'package:library_base/widget/image/local_image.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:module_wms/page/outbound_orders/as_order/as_order_controller.dart';

Widget buildSearchWidget(
  AsOrderController asOrderController,
    {
      ValueChanged<String>? onChanged,
      ValueChanged<String>? onSearch,
    }
) {
  return Container(
    color: ThemeUtil.getTheme().colorScheme.secondary,
    height: 116.w,
    child: Row(
      children: <Widget>[
        Expanded(
          child: Container(
              alignment: Alignment.center,
              height: double.infinity,
              width: double.infinity,
              margin: EdgeInsets.only(left: 24.w, top: 24.w, bottom: 24.w),
              decoration: BoxDecoration(
                color: ThemeUtil.getTheme().secondaryBgColor(),
                borderRadius: const BorderRadius.all(Radius.circular(30.0)),
              ),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  gapW24,
                  LocalImage('common_search',
                      package: Constant.packLib(), width: 26.w, height: 26.w),
                  gapW24,
                  Expanded(
                    child: Container(
                      alignment: Alignment.center,
                      height: double.infinity,
                      width: double.infinity,
                      child: TextField(
                        onChanged: onChanged,
                        style: normalTextColor28,
                        autofocus: false,
                        controller: asOrderController.keyWorkSearchController,
                        textAlignVertical: TextAlignVertical.center,
                        decoration: InputDecoration(
                          focusedBorder: const OutlineInputBorder(
                              borderSide: BorderSide(
                                  width: 0, color: Colors.transparent)),
                          disabledBorder: const OutlineInputBorder(
                              borderSide: BorderSide(
                                  width: 0, color: Colors.transparent)),
                          enabledBorder: const OutlineInputBorder(
                              borderSide: BorderSide(
                                  width: 0, color: Colors.transparent)),
                          contentPadding:
                              const EdgeInsets.symmetric(vertical: 0),
                          border: InputBorder.none,
                          labelStyle: TextStyles.textNormal28,
                          hintText: MultiLanguage.pleaseEnterNumber.tr,
                          hintStyle: TextStyles.textSixthColor28,
                          suffixIcon:
                          asOrderController.keyWord.isNotEmpty
                              ? IconButton(
                            iconSize: 30.w,
                            icon: Icon(Icons.close,
                                color: Colours.gray_400, size: 40.w),
                            onPressed: () {
                              asOrderController.keyWorkSearchController.text = '';
                              asOrderController.keyWord.value = "";
                              asOrderController.update();
                            },
                          )
                              : gapZero,
                        ),
                      ),
                    ),
                  ),
                  Container(
                    padding: EdgeInsets.only(right: 24.w),
                    child: LocalImage('wms_scan',
                        package: Constant.packWms(), width: 32.w, height: 32.w),
                  ),
                ],
              )),
        ),
        textInkBgWell(MultiLanguage.search.tr, onTap: (action) {
          onSearch?.call(action);
        })
      ],
    ),
  );
}

Widget buildBottom(
    double finishedCount,
    double totalCount,
    bool enable,
    bool isSelectedAll,
    int selectCount,
    {
      void Function()? print,
      void Function()? deliverGoods,
      void Function()? selectedAll,
    }) {
  return Container(
    color: Colors.white,
    child: Column(
      children: [
        Container(
            height: 166.w,
            decoration: rectTop,
            alignment: Alignment.topRight,
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Expanded(child: Container(
                  color: Colors.transparent,
                  child: Stack(
                    children: [
                      Positioned(
                        top: 47.w,
                        left: 24.w,
                        child: GestureDetector(
                          onTap: (){
                            selectedAll?.call();
                          },
                          child: Row(
                            mainAxisSize: MainAxisSize.min,
                            children: [
                              LocalImage(
                                  isSelectedAll
                                      ? 'admin_checked'
                                      : 'admin_no_checked',
                                  package: Constant.packAdmin(),
                                  width: 34.r,
                                  height: 34.r),
                              gapW18,
                              RichText(
                                text: TextSpan(
                                    text: MultiLanguage.selectedOrderCount
                                        .trArgs(["$selectCount"]),
                                    style: TextStyles.textNormal28,
                                ),
                              ),
                            ],
                          ),
                        ),
                      ),
                    ],
                  ),
                )),
                Visibility(
                  visible: true,
                  child: GradientButton(
                    ThemeUtil.getTheme(),
                    decoration: BoxDecoration(
                        borderRadius: const BorderRadius.all(Radius.circular(20)),
                        border: Border.fromBorderSide(BorderSide(color: ThemeUtil.getTheme().sixthTextColor())),
                        color: ThemeUtil.getTheme().colorScheme.secondary),
                    normalColor:ThemeUtil.getTheme().colorScheme.secondary,
                    textStyle: enable?normalTextColor28:secondaryTextColor28,
                    height: 77.w,
                    width: 184.w,
                    margin: EdgeInsets.only(right: 16.w, top: 21.w),
                    padding: EdgeInsets.symmetric(horizontal: 30.w),
                    text: MultiLanguage.print.tr,
                    onPressed:enable?print:null,
                  ),
                ),
                GradientButton(
                  ThemeUtil.getTheme(),
                  height: 77.w,
                  width: 184.w,
                  margin: EdgeInsets.only(right: 24.w, top: 21.w),
                  padding: EdgeInsets.symmetric(horizontal: 30.w),
                  text: MultiLanguage.sendOut.tr,
                  onPressed:enable?deliverGoods:null,
                ),
              ],
            )),
      ],
    ),
  );
}

Widget buildItem(
  StockOutChildEntity taskEntity,
  int position, {
    Function(StockOutChildEntity,int)? onCheck,
  Function? onExpand,
  Function(StockOutChildEntity taskEntity, int position)? toOrderDetail,
}) {
  List<Widget> widgets = [];

  ///第一行内容
  widgets.add(GestureDetector(
    onTap: () {
      if(taskEntity.status == StockOutOrderStatus.FINISHED.name){
        onCheck?.call(taskEntity,position);
      }
    },
    child: Container(
      color: Colors.transparent,
      padding: edgeAll24,
      child: Row(
        children: [
          Container(
            color: ThemeUtil.getTheme().colorScheme.secondary,
            child: Visibility(
              visible: taskEntity.status == StockOutOrderStatus.FINISHED.name,
              child: LocalImage(taskEntity.isSelected ? 'common_checked' : 'common_no_checked',
                      package: Constant.packLib(), width: 34.r, height: 34.r),
            ),
          ),
          Padding(
              padding: EdgeInsets.only(left: taskEntity.status == StockOutOrderStatus.FINISHED.name?10.w:0),
              child: Text(taskEntity.orderNumber, style: normalTextColor28)),
          Expanded(
              child: Text(getStatusName(taskEntity),
                  style: getStatusTextStyle(taskEntity),textAlign: TextAlign.end,))
        ],
      ),
    ),
  ));
  widgets.add(line1);
  var infoWidget = Padding(
    padding: edgeLR24,
    child: Row(
        children: [
      Expanded(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            gapH18,
            Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text("${MultiLanguage.consignee.tr} : ", style: normalTextColor28),
                Expanded(
                    child: Text(taskEntity.consignee, style: normalTextColor28)),
              ],
            ),
            gapH18,
            Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text("${MultiLanguage.shortName.tr} : ", style: normalTextColor28),
                Expanded(
                    child: Text(taskEntity.address, style: normalTextColor28_2)),
              ],
            ),
            gapH18,
            Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text("${MultiLanguage.commodityQuantity.tr} : ",
                    style: normalTextColor28),
                Expanded(
                    child:
                        Text(taskEntity.orderCount, style: normalTextColor28)),
              ],
            ),
            gapH18,
            Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text("${MultiLanguage.deliveryTime.tr} : ",
                    style: normalTextColor28),
                Expanded(
                    child:
                    Text(taskEntity.stationTime, style: normalTextColor28)),
              ],
            ),
          ],
        ),
      ),
      Visibility(
          visible: taskEntity.status == StockOutOrderStatus.FINISHED.name,
          child: LocalImage('wms_complete',
              package: Constant.packWms(), width: 92.w, height: 92.w))
    ]),
  );
  widgets.add(infoWidget);
  widgets.add(gapH24);
  return GestureDetector(
    onTap: () {
      toOrderDetail?.call(taskEntity,position);
    },
    child: roundWidget(
      margin: edgeLR24,
      decoration: rs8,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: widgets,
      ),
    ),
  );
}

String getStatusName(StockOutChildEntity taskEntity) {
  if (taskEntity.status == StockOutOrderStatus.NOT_ASSIGN.name) {
    return MultiLanguage.undistributed.tr;
  } else if (taskEntity.status == StockOutOrderStatus.ASSIGNED.name) {
    return MultiLanguage.allocated.tr;
  } else {
    return MultiLanguage.weighed.tr;
  }
}

TextStyle getStatusTextStyle(StockOutChildEntity taskEntity) {
  if (taskEntity.status == StockOutOrderStatus.NOT_ASSIGN.name) {
    return fourthTextColor28;
  } else if (taskEntity.status == StockOutOrderStatus.ASSIGNED.name) {
    return primaryTextColor28;
  } else {
    return normalTextColor28;
  }
}




